#pragma once
#ifndef XEGE_PAINT
#define XEGE_PAINT
#include "xege.h"
// #include "font.h"
// #include "image_base.h"
// #include "paint.h"
namespace xege {

#define XR XEGEGET_R
#define XG XEGEGET_G
#define XB XEGEGET_B
#define XA XEGEGET_A
class PAINT : public IMAGE_BASE/*, FONT*/ {
private:
	// IMAGE *temp = new IMAGE ()
public:
	PAINT ();
	
	PAINT& xyprintf (std::string str , int x, int y, COLOR C = { 0xff, 0xff, 0xff, 0xff });
	PAINT& outtextxy (std::string str, int x, int y, COLOR C = { 0xff, 0xff, 0xff, 0xff });

	// RF 是 Really Fast 的意思
	PAINT& drawPixelRF (short x, short y, color_t c);
	PAINT& drawPixelRF (short x, short y, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
	PAINT& drawPixelRF (short x, short y);
	PAINT& drawFillRectRF (short x, short y, short w, short h);
	// SDL_RenderDrawLine
	// SDL_RenderDrawLines
	// SDL_RenderDrawPoints
	// SDL_RenderDrawRects


	// 画一个点
	PAINT& drawPixel (short x, short y);
	PAINT& drawPixel (short x, short y, color_t C);
	PAINT& drawPixel (short x, short y, COLOR C);
	PAINT& putPixel (short x, short y);
	PAINT& putPixel (short x, short y, color_t C);
	PAINT& putPixel (short x, short y, COLOR C);



	int capsule(float px, float py, float ax, float ay, float bx, float by, float r) {
		float pax = px - ax, pay = py - ay, bax = bx - ax, bay = by - ay;
		float h = fmaxf(fminf((pax * bax + pay * bay) / (bax * bax + bay * bay), 1.0f), 0.0f);
		float dx = pax - bax * h, dy = pay - bay * h;
		return dx * dx + dy * dy < r * r;
	}

	float capsuleSDF(float px, float py, float ax, float ay, float bx, float by, float r) {
		float pax = px - ax, pay = py - ay, bax = bx - ax, bay = by - ay;
		float h = fmaxf(fminf((pax * bax + pay * bay) / (bax * bax + bay * bay), 1.0f), 0.0f);
		float dx = pax - bax * h, dy = pay - bay * h;
		return sqrtf(dx * dx + dy * dy) - r; // 只改变这句及返回类型
	}
	PAINT& alphablend(int x, int y, float alpha, float r, float g, float b) {
		drawPixel (x,y, XEGERGBA(
			(unsigned char)(r * alpha * 255),
			(unsigned char)(g * alpha * 255),
			(unsigned char)(b * alpha * 255),
			(unsigned char)(1 - alpha)
		));
	}

	void lineSDFAABB(float ax, float ay, float bx, float by, float r, color_t C) {
		int x0 = (int)floorf(fminf(ax, bx) - r);
		int x1 = (int) ceilf(fmaxf(ax, bx) + r);
		int y0 = (int)floorf(fminf(ay, by) - r);
		int y1 = (int) ceilf(fmaxf(ay, by) + r);
		for (int y = y0; y <= y1; y++)
			for (int x = x0; x <= x1; x++)
				alphablend(x, y, fmaxf(fminf(0.5f - capsuleSDF(x, y, ax, ay, bx, by, r), 1.0f), 0.0f), XR(C), XG(C), XB(C));
	}

/*
linestyle
线型，可以是以下值：
值	含义
SOLID_LINE	线形为实线。
CENTER_LINE	线形为：－－－－－－－－－－－－
DOTTED_LINE	线形为：●●●●●●●●●●●●
DASHED_LINE	线形为：－●－●－●－●－●－●
NULL_LINE	线形为不可见。
USERBIT_LINE	线形样式是自定义的，依赖于 upattern 参数。


fillstyle
填充类型

*/

	// 画一条横线
	PAINT& drawLineH (short x1, short x2, short y);
	PAINT& drawLineH (short x1, short x2, short y, color_t C);
	PAINT& drawLineH (short x1, short x2, short y, COLOR C);
	// 画一条竖线
	PAINT& drawLineV (short x, short y1, short y2);
	PAINT& drawLineV (short x, short y1, short y2, color_t C);
	// 画一条线
	PAINT& drawLine (short x1, short y1, short x2, short y2);
	PAINT& drawLine (short x1, short y1, short x2, short y2, color_t C);
	// 画一条抗锯齿线
	PAINT& drawLineAA (short x1, short y1, short x2, short y2);
	PAINT& drawLineAA (short x1, short y1, short x2, short y2, color_t C);
	// 有宽度的线
	PAINT& drawLineW (short x1, short y1, short x2, short y2, Uint8 width);
	PAINT& drawLineW (short x1, short y1, short x2, short y2, Uint8 width, color_t C);


	// 画一个矩形
	PAINT& drawRect (short x1, short y1, short x2, short y2);
	PAINT& drawRect (short x1, short y1, short x2, short y2, color_t C);
	// 画一个圆角矩形
	PAINT& drawRoundRect (short x1, short y1, short x2, short y2, int rad);
	PAINT& drawRoundRect (short x1, short y1, short x2, short y2, int rad, color_t C);
	// 画一个填充矩形
	PAINT& drawFillRect (short x1, short y1, short x2, short y2);
	PAINT& drawFillRect (short x1, short y1, short x2, short y2, color_t C);
	// using drawBox = drawFillRect;
	// 画一个填充圆角矩形
	PAINT& drawFillRoundRect (short x1, short y1, short x2, short y2, int rad);
	PAINT& drawFillRoundRect (short x1, short y1, short x2, short y2, int rad, color_t C);


	// 画三角形（线）
	PAINT& drawTrigon (short x1, short y1, short x2, short y2, short x3, short y3);
	PAINT& drawTrigon (short x1, short y1, short x2, short y2, short x3, short y3, color_t C);
	// 画抗锯齿三角形
	PAINT& drawTrigonAA (short x1, short y1, short x2, short y2, short x3, short y3);
	PAINT& drawTrigonAA (short x1, short y1, short x2, short y2, short x3, short y3, color_t C);
	// 画填充三角形
	PAINT& drawFillTrigon (short x1, short y1, short x2, short y2, short x3, short y3);
	PAINT& drawFillTrigon (short x1, short y1, short x2, short y2, short x3, short y3, color_t C);

	// 画多边形（线）
	PAINT& drawPoly (short* px, short* py, uint n);
	PAINT& drawPoly (short* px, short* py, uint n, color_t C);
	// 画抗锯齿多边形（线）
	PAINT& drawPolyAA (short* px, short* py, uint n);
	PAINT& drawPolyAA (short* px, short* py, uint n, color_t C);
	// 画填充多边形
	PAINT& drawFillPoly (short* px, short* py, uint n);
	PAINT& drawFillPoly (short* px, short* py, uint n, color_t C);
	/*
	** 很难搞阿，我暂时还不是很确定这个 surface 用啥来传……
	PAINT& drawTexturePoly (short* px, short* py, uint n, SDL_Surface* texture, int dx, int dy) {
		texturedPolygon (rPic, px, py, n, texture, dx, dy);
	}
	PAINT& drawTexturePoly (short* px, short* py, uint n, SDL_Surface &texture, int dx, int dy) {
		texturedPolygon (rPic, px, py, n, &texture, dx, dy);
	}
	*/
	PAINT& drawArc (short x, short y, short rad, short start, short end);
	PAINT& drawArc (short x, short y, short rad, short start, short end, color_t C);
	// 画一个椭圆
	PAINT& drawEsplise (short left, short top, short right, short bottom);
	PAINT& drawEsplise (short left, short top, short right, short bottom, color_t C);
	// 画一个抗锯齿椭圆
	PAINT& drawEspliseAA (short left, short top, short right, short bottom);
	PAINT& drawEspliseAA (short left, short top, short right, short bottom, color_t C);
	// 画一个填充椭圆
	PAINT& drawFillEsplise (short left, short top, short right, short bottom);
	PAINT& drawFillEsplise (short left, short top, short right, short bottom, color_t C);


	// 画一个圆
	PAINT& drawCircle (short x, short y, short r);
	PAINT& drawCircle (short x, short y, short r, color_t C);
	// 画一个抗锯齿圆
	PAINT& drawCircleAA (short x, short y, short r);
	PAINT& drawCircleAA (short x, short y, short r, color_t C);
	// 画一个填充圆
	PAINT& drawFillCircle (short x, short y, short r);
	PAINT& drawFillCircle (short x, short y, short r, color_t C);


	// 画扇形线（圆心坐标，半径，开始位置，结束位置（角度制，0 为右，90 为下，-90 为上））
	PAINT& drawPie (short x, short y, short rad, short start, short end);
	PAINT& drawPie (short x, short y, short rad, short start, short end, color_t C);
	// 画填充扇形（圆心坐标，半径，开始位置，结束位置（角度制，0 为右，90 为下，-90 为上））
	PAINT& drawFillPie (short x, short y, short rad, short start, short end);
	PAINT& drawFillPie (short x, short y, short rad, short start, short end, color_t C);

	// 画贝塞尔曲线
	PAINT& drawBezier (short* px, short* py, uint n, uint s);
	PAINT& drawBezier (short* px, short* py, uint n, uint s, color_t C);

	PAINT& setDrawFont (const void *fontdata, Uint32 cw, Uint32 ch);
	PAINT& setDrawFontRotation (Uint32 rotation);
	PAINT& drawChar (short x, short y, char c);
	PAINT& drawChar (short x, short y, char c, color_t C);
	PAINT& drawString (short x, short y, std::string str);
	PAINT& drawString (short x, short y, std::string str, color_t C);
	PAINT& drawString (short x, short y, const char* str);
	PAINT& drawString (short x, short y, const char* str, color_t C);

};  // 类 PAINT 结尾
}  // 命名空间 xege 结尾
#endif